home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / cross / GBDK-2.0.lha / GBDK / lib / arand.s < prev    next >
Text File  |  1998-10-01  |  3KB  |  120 lines

  1. ;/***************************************************************************
  2. ; *                                                                         *
  3. ; * Module  : arand.s                                                       *
  4. ; *                                                                         *
  5. ; * Purpose : A random number generator using the lagged additive method    *
  6. ; *                                                                         *
  7. ; * Version : 1, January 11 1998                                            *
  8. ; *                                                                         *
  9. ; * Author  : Luc Van den Borre ( Homepage : NOC.BASE.ORG )                 *
  10. ; *                                                                         *
  11. ; **************************************************************************/
  12.  
  13.     .include    "global.s"
  14.  
  15.     .globl    .initrand
  16.     .globl    _rand
  17.  
  18.     .area    _BSS
  19. .randarr:
  20.     .ds    55
  21. .raxj:
  22.     .ds    0x01
  23. .raxk:
  24.     .ds    0x01
  25.  
  26.     .area    _CODE
  27.  
  28.     ;; arand() operates on an array of 55 arbitrary values (here : bytes).
  29.     ;; It adds two values of the array together, replaces one of the values
  30.     ;; with the result, and returns the result.
  31.     ;; At start, the indices into the array refer to the 55th and 24th element.
  32.     ;; After each call, each index is decreased, and looped around if necessary.
  33.     ;; This kind of works, but the values produces are less good than those by
  34.     ;; rand(), mainly because it operates on bytes instead of words.
  35.     ;; Ref : D. E. Knuth, "The Art of Computer Programming" , Volume 2
  36.     ;;
  37.     ;; Exit conditions
  38.     ;;   DE = Random number (byte!)
  39.     ;;
  40.     ;; Registers used:
  41.     ;;   all
  42.     ;;
  43. _arand::
  44.     PUSH    BC
  45.     LD    D, #0
  46.     LD    HL, #.randarr-1
  47.     LD    A, (.raxj)
  48.     LD    E, A
  49.     DEC    A        ; Decrease the pointer
  50.     JR    NZ, 1$
  51.     LD    A, #55
  52. 1$:
  53.     LD    (.raxj), A
  54.     ADD    HL, DE
  55.     LD    B, (HL)
  56.  
  57.     LD    HL, #.randarr-1    ; Ooh...
  58.     LD    A, (.raxk)
  59.     LD    E, A
  60.     DEC    A        ; Decrease the pointer
  61.     JR    NZ, 2$
  62.     LD    A, #55
  63. 2$:
  64.     LD    (.raxk), A
  65.     ADD    HL, DE
  66.     LD    A, (HL)
  67.  
  68.     ADD    A, B
  69.     LD    (HL), A        ; Store new value
  70.  
  71.     POP    BC
  72.  
  73.     LD    D, #0
  74.     LD    E, A
  75.  
  76.     RET
  77.  
  78.     ;; _initarand calls the _rand function to fill the array with random values
  79.     ;; Note that this also sets the seed value of the _rand function first,
  80.     ;; like _initrand
  81.     ;;
  82.     ;; Exit conditions
  83.     ;;   None
  84.     ;;
  85.     ;; Registers used:
  86.     ;;   all
  87.     ;;
  88. _initarand::
  89.     LDA    HL,2(SP)
  90.     CALL    .initrand
  91.  
  92.     PUSH    BC
  93.     LD    A, #55
  94.     LD    HL, #.randarr
  95. 1$:
  96.     DEC    A
  97.     LD    (.raxj), A
  98.     LD    B, H
  99.     LD    C, L
  100.     CALL    _rand
  101.     LD    H, B
  102.     LD    L, C
  103.  
  104.     LD    (HL), D
  105.     INC    HL
  106.     LD    (HL), E
  107.     INC    HL
  108.     
  109.     LD    A, (.raxj)
  110.     CP    #0
  111.     JR    NZ, 1$
  112.  
  113.     LD    A, #24        ; Now the array has been filled, set the pointers
  114.     LD    (.raxj), A
  115.     LD    A, #55
  116.     LD    (.raxk), A
  117.  
  118.     POP    BC
  119.     RET
  120.